package cn.jas.common.jascommon.util;

import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.InvocationTargetException;

/**
 * @description: 公共的工具类的实现
 * @author: chen_mx
 **/
public class CommonUtil {


    public   static HttpServletRequest    getRequest(){
        RequestAttributes bute= RequestContextHolder.getRequestAttributes();
        if(bute instanceof ServletRequestAttributes){
            HttpServletRequest   request=((ServletRequestAttributes) bute).getRequest();
            if(request.getAttribute("multipartRequest") != null){
                return (HttpServletRequest) request.getAttribute("multipartRequest");
            }else {
                return  ((ServletRequestAttributes) bute).getRequest();
            }
        }else if(bute instanceof  ServletRequestAttributes){
            HttpServletRequest  request= ((ServletRequestAttributes) bute).getRequest();
            if(request.getAttribute("multipartRequest")!=null){
                return (HttpServletRequest) request.getAttribute("multipartRequest");
            }else{
               return  ((ServletRequestAttributes) bute).getRequest();
            }
        }
        return  null;
    }


    public  static HttpServletResponse  getResponse(){
        RequestAttributes attributes= RequestContextHolder.getRequestAttributes();
            try {
                if(attributes instanceof ServletRequestAttributes){
                    return (HttpServletResponse) attributes.getClass().getMethod("getResponse").invoke(attributes);
                }else if (attributes instanceof ServletRequestAttributes){
                    return ((ServletRequestAttributes)attributes).getResponse();
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            }
        return null;
    }


    public static void main(String[] args) {

        String  str="ceshi1";
        System.out.println(str.indexOf("T=")!=0);


    }
}
